package com.huodongrili.calender.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.huodongrili.calender.data.DBTables.Comment;
import com.huodongrili.calender.data.DBTables.Feedback;
import com.huodongrili.calender.data.DBTables.Lunar;
import com.huodongrili.calender.data.DBTables.Sort;
import com.huodongrili.calender.data.DBTables.TEvents;

public class DBHelper extends SQLiteOpenHelper {

	private static DBHelper mInstance = null;

	public static final String DATABASE_NAME = "aec";
	
	public static final int DATABASE_VERSION = 1;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public static DBHelper getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new DBHelper(context);
		}
		return mInstance;
	}


	public static SQLiteDatabase getWriteDatabase(Context context) {

		SQLiteDatabase db = getInstance(context).getWritableDatabase();
		if (!db.isOpen()) {
			db.openDatabase(DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
		}
		return db;

	}

	public static SQLiteDatabase getReadDatabase(Context context) {

		SQLiteDatabase db = getInstance(context).getReadableDatabase();
		if (!db.isOpen()) {
			db.openDatabase(DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
		}
		return db;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		
		createCommentTable(db);
		createFeedbackTable(db);
		//createEventsTable(db);
		//createSortTable(db);
		//createLunarTable(db);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	
	private void createSortTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + Sort.TABLE_NAME
				+ " (" + Sort._ID + " INTEGER PRIMARY KEY,"
				+ Sort.CODE + " INTEGER DEFAULT 0,"
				+ Sort.NAME + " TEXT," + Sort.ENABLE
				+ " INTEGER DEFAULT 1," + Sort.DESC + " TEXT);");
	}
	
	private void createLunarTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + Lunar.TABLE_NAME
				+ " (" + Lunar._ID + " INTEGER PRIMARY KEY,"
				+ Lunar.DATE + " TEXT," + Sort.DESC + " TEXT);");
	}	
	
	
	private void createCommentTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + Comment.TABLE_NAME
				+ " (" + Comment._ID + " INTEGER PRIMARY KEY,"
				+ Comment.DATELONG + " LONG,"
				+ Comment.E_ID + " TEXT," + Comment.CHECKED
				+ " INTEGER DEFAULT 1," + Comment.LIKE + " INTEGER);");
	}
	
	
	private void createFeedbackTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + Feedback.TABLE_NAME
				+ " (" + Feedback._ID + " INTEGER PRIMARY KEY,"
				+ Feedback.CONTENT + " TEXT,"
				+ Feedback.DESC + " TEXT," + Feedback.DATELONG + " LONG);");
	}	
	
	
	private void createEventsTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TEvents.TABLE_NAME
				+ " (" + TEvents._ID + " INTEGER PRIMARY KEY,"
				+ TEvents.ID + " TEXT,"
				+ TEvents.S_TIME + " TEXT,"
				+ TEvents.E_TIME + " TEXT," 
				+ TEvents.TITLE + " TEXT," 
				+ TEvents.CONTENT + " TEXT," 
				+ TEvents.CITY_CODE + " TEXT," 
				+ TEvents.SORT + " TEXT," 
				+ TEvents.POINT + " INTEGER DEFAULT 0," 
				+ TEvents.ADDR + " TEXT," 
				+ TEvents.DATE + " TEXT," 
				+ TEvents.END_DATE + " TEXT," 
				+ TEvents.SRC + " TEXT," 
				+ TEvents.LINK + " TEXT," 
				+ TEvents.IYEAR + " INTEGER DEFAULT 2013,"
				+ TEvents.IMONTH + " INTEGER DEFAULT 1,"
				+ TEvents.IDAY + " INTEGER DEFAULT 1);");
	}

}